package com.test.springbootdemos.mybatisplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.springbootdemos.mybatisplus.entity.User;
import com.test.springbootdemos.mybatisplus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(当前类名.class);
可以用注解@Slf4j;
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    public List<User> getAll() {
        List<User> list = userService.list();
        System.out.println("list:" + list);
        return list;
    }
    @RequestMapping("/getPage")
    public IPage<User> getPage(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize,
                               HttpServletRequest req) {
        //需要在Config配置类中配置分页插件
        Page<User> page = new Page<>(pageNo, pageSize);
//        page.setCurrent(5); //当前页
//        page.setSize(1);    //每页条数
        IPage<User> userList = userService.page(page);
        log.info("查询当前页：" + userList.getCurrent());
        log.info("查询当前页数量：" + userList.getSize());
        log.info("查询结果数量：" + userList.getRecords().size());
        log.info("数据总数：" + userList.getTotal());
        log.info("是否有下一页：" + page.hasNext());

        /*
        在java web项目中虽然可以通过request.getParameterMap()很轻松的获得参数Map，
        但得到的Map和普通Map是不一样的，是被锁定的，
        不能像操作常规Map那样进行put、get等操作，该方法将得到参数Map返回为可操作的普通Map
         */
        log.info("req.getParameterMap()"+req.getParameterMap());

        Map<String, String[]> paramMap = req.getParameterMap();
        Set<String> keys = paramMap.keySet();
        for(String key:keys){
            log.info(key+":"+ Arrays.toString((String[])paramMap.get(key)));
        }




        return userList;

//        QueryWrapper<JeecgDemo> queryWrapper = QueryGenerator.initQueryWrapper(jeecgDemo, req.getParameterMap());
//        Page<JeecgDemo> page = new Page<JeecgDemo>(pageNo, pageSize);
//
//        IPage<JeecgDemo> pageList = jeecgDemoService.page(page, queryWrapper);
//        log.info("查询当前页：" + pageList.getCurrent());
//        log.info("查询当前页数量：" + pageList.getSize());
//        log.info("查询结果数量：" + pageList.getRecords().size());
//        log.info("数据总数：" + pageList.getTotal());
//        return Result.ok(pageList);
    }





}
